package com.wangcanxuan.www.util.pool.total;

import com.wangcanxuan.www.util.SecurityUtil;

import java.time.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
 * 连接统计类
 *
 * @author HHLJ
 * @date 2022/03/24
 */
public class ConnectionsTotal {

    /**
     * 连接池创建日期
     */
    private static LocalDateTime localDateTime;
    /**
     * 被创建的连接数量
     */
    private static final AtomicInteger CREATED_CONNECTIONS = new AtomicInteger(0);

    /**
     * 被关闭的连接数量
     */
    private static final AtomicInteger CLOSED_CONNECTIONS = new AtomicInteger(0);

    /**
     * 被检查出不可用的连接数量
     */
    private static final AtomicInteger CHECKED_UNAVAILABLE_CONNECTIONS = new AtomicInteger(0);

    /**
     * 被检测出的超时连接数量
     */
    private static final AtomicInteger CHECK_OUT_CONNECTIONS = new AtomicInteger(0);

    public static void setCreatedTime() {
        localDateTime = LocalDateTime.now();
    }

    /**
     * 连接池统计日志打印
     *
     * @param logger 日志记录器
     */
    public static void poolTotal(Logger logger) {
        logger.info("连接池已创建时长：" + SecurityUtil.getTime(localDateTime));
        logger.info("被创建的连接总数量:" + CREATED_CONNECTIONS.get());
        logger.info("被关闭的连接总数量:" + CLOSED_CONNECTIONS.get());
        logger.info("被检查出不可用的连接总数量:" + CHECKED_UNAVAILABLE_CONNECTIONS.get());
        logger.info("被检测出的超时连接总数量:" + CHECK_OUT_CONNECTIONS.get());
    }


    public static void createdConnectionsIncrement() {
        CREATED_CONNECTIONS.incrementAndGet();
    }

    public static void closedConnectionsIncrement() {
        CLOSED_CONNECTIONS.incrementAndGet();
    }

    public static void checkedUnavailableConnectionsIncrement() {
        CHECKED_UNAVAILABLE_CONNECTIONS.incrementAndGet();
    }

    public static void checkOutConnectionsIncrement() {
        CHECK_OUT_CONNECTIONS.incrementAndGet();
    }

}
